package com.ccx.coachsite.service;

import java.util.ArrayList;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

import com.ccx.coachsite.R;
import com.ccx.coachsite.api.CoachSiteSoftMgrApiAccessor;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.bus.TutorialInfo;
import com.ccx.coachsite.model.CSResultCodeMap;
import com.ccx.coachsite.model.ServerResult;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.ui.softmgr.SoftMgrDetailActivity;
import com.ccx.coachsite.ui.softmgr.SoftMgrTopicWebDetail;
import com.ccx.coachsite.ui.tutorial.TutorialDetailActivity;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class GetShareMessageTask extends AsyncTask<Object, Integer, String>{

	private final static String TAG = GetShareMessageTask.class.getSimpleName();

	private Context context = null;
	
	private ServerResult<SoftShareMessage> serverResult;
	
	public GetShareMessageTask(Context ctx){
		this.context = ctx;
	}
	
	@Override
	protected String doInBackground(Object... params) {

		//获取登录这的mobile		
		//Account acount = AccountMgr.readAccountInfo(context);
		//String mobile = acount.getMobile();
		
		String userId = SharedPreferencesUtil.getValue(context,
				SharedKey.Shared_UserId_Key, "");
		if(StringUtil.isEmpty(userId))
			return "";
		
		String mobile = SharedPreferencesUtil.getValue(context,
				SharedKey.Shared_Mobile_Key, "");
		
		serverResult = CoachSiteSoftMgrApiAccessor.getShareMessage(mobile);
		
		if ( serverResult.isbNetworkProblem() ){
			//网络错误
			Log.d(TAG, "网络错误");
		}else{
			if ( serverResult.getResultCode()==CSResultCodeMap.APP_SUCCESS_CODE ){
				//构造通知消息
			}else{
				Log.d(TAG, "服务的返回错误:"+ serverResult.getResultCode());
			}
		}
		
		//Log.d(TAG, "网络 GetShareMessageTask mobile="+mobile);
				
		return serverResult.getResultCode()+"";
	}

	@Override
	protected void onPostExecute(String result) {

		if ( (CSResultCodeMap.APP_SUCCESS_CODE+"").equals(result)) {
			ArrayList<SoftShareMessage> listShareMessage = serverResult.itemList;
			//Log.d(TAG, "网络 listShareMessage.size()="+listShareMessage.size());
			if ( listShareMessage!=null && listShareMessage.size()!=0 ){
				NotificationManager nManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
				
				for (int i = 0; i < listShareMessage.size(); i++) {
					SoftShareMessage softShareMessage =listShareMessage.get(i);
					sendNotification(nManager, softShareMessage);
				}
			}
		}
		super.onPostExecute(result);
	}
	
	private void sendNotification(NotificationManager nManager, SoftShareMessage softShareMessage){
		
		//shareType   应用类型  1.教程 2应用 3游戏 4专题
		
		Notification notif = new Notification( R.drawable.icon, 
				context.getString( R.string.soft_mgr_share_notify_content, softShareMessage.getContent() ), 
				System.currentTimeMillis() );
		int tmpID = 100000;
		Intent mIntent = new Intent();
		int iType = 1;
		try {
			iType = Integer.parseInt(softShareMessage.getType());
		}catch(Exception ex){
			ex.printStackTrace();
		}
			
		switch(iType){
			case SoftShareMessage.TYPE_TUTORIAL_SHARE_TYPE:
				tmpID = 100000;
				mIntent.setClass(context,TutorialDetailActivity.class);
				TutorialInfo info = new TutorialInfo();
				info.setId(Integer.parseInt(softShareMessage.getTypeId()));
				mIntent.putExtra("tutorial", info);
				mIntent.putExtra("ShareCount", softShareMessage.getId());
				break;
			case SoftShareMessage.TYPE_APP_SHARE_TYPE:
				tmpID = 200000;
				mIntent.setClass(context,SoftMgrDetailActivity.class);
				mIntent.putExtra("softID",Integer.parseInt(softShareMessage.getTypeId()));
				mIntent.putExtra("ShareCount", softShareMessage.getId());
				break;
			case SoftShareMessage.TYPE_GAME_SHARE_TYPE:
				tmpID = 300000;
				mIntent.setClass(context,SoftMgrDetailActivity.class);
				mIntent.putExtra("softID",Integer.parseInt(softShareMessage.getTypeId()));
				mIntent.putExtra("ShareCount", softShareMessage.getId());
				break;
			case SoftShareMessage.TYPE_TOPIC_SHARE_TYPE:
				tmpID = 400000;
				mIntent.setClass(context,SoftMgrTopicWebDetail.class);
				mIntent.putExtra("TopicID",softShareMessage.getTypeId());
				mIntent.putExtra("ShareCount", softShareMessage.getId());
				break;
			default:
				break;
		}
			
		PendingIntent pIntent = PendingIntent.getActivity(context, tmpID+Integer.parseInt(softShareMessage.getTypeId()), mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		notif.flags = Notification.FLAG_AUTO_CANCEL;
        notif.setLatestEventInfo(context, context.getResources().getString(R.string.main_bottom_app_name), softShareMessage.getContent(), pIntent);
        nManager.notify(R.string.main_bottom_app_name+tmpID+Integer.parseInt(softShareMessage.getTypeId()), notif); 
	}
}
